/*
 * Copyright (c) 2009, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "tssp_internal.h"
#include "tssp_table.h"
#include "tssp_pmt.h"


struct tssp_pmt_monitor {
    uint16_t program_number;
    struct tssp_table_monitor *tmon;
    tssp_pmt_monitor_callback callback;
    void *userdata;
};


struct tssp_pmt *tssp_pmt_parse(const struct tssp_section *section)
{
    struct tssp_pmt *pmt = NULL;
    struct tssp_pmt_es *es;
    uint16_t length;
    uint8_t *pos;
    int ret;
    struct tssp_table_payload p;

    TSSP_ASSERT(section);

    if (section->table_id != TSSP_TABLE_ID_PMT || !section->syntax_indicator)
        return NULL;

    /* prepare data buffer */
    ret = tssp_table_get_payload(&p, section, 0);
    if (ret != TSSP_STATUS_OK)
        return NULL;
    pos = p.start;

    if ((pos + 4) > p.end)
        goto cleanup;

    /* allocate pmt */
    pmt = tssp_pmt_create(section->extension,
                          section->version,
                          section->current_next,
                          ((0x1F & pos[0]) << 8) + pos[1]);
    if (pmt == NULL)
        goto cleanup;

    length = ((0x0F & pos[2]) << 8) + pos[3]; /* program_info_length */
    pos += 4;

    /* descriptors */
    if (tssp_descriptor_parse(&pmt->descriptors, pos, p.end, length) < 0)
        goto cleanup;
    pos += length;

    /* es info */
    while ((p.end - pos) >= 5) {
        /* add elementary stream */
        es = tssp_pmt_add_es(pmt, pos[0], ((0x1F & pos[1]) << 8) + pos[2]);
        if (es == NULL)
            goto cleanup;

        length = ((0x0F & pos[3]) << 8) + pos[4]; /* ES_info_length */
        pos += 5;
        if (tssp_descriptor_parse(&es->descriptors, pos, p.end, length) < 0)
            goto cleanup;
        pos += length;
    }

    tssp_table_free_payload(&p);
    return pmt;

cleanup:
    tssp_pmt_destroy(pmt);
    tssp_table_free_payload(&p);
    return NULL;
}

struct tssp_pmt *tssp_pmt_create(uint16_t program_number,
                                 uint8_t version,
                                 uint8_t current_next,
                                 uint16_t pcr_pid)
{
    struct tssp_pmt *pmt;

    pmt = malloc(sizeof(*pmt));
    if (pmt) {
        STAILQ_INIT(&pmt->descriptors);
        STAILQ_INIT(&pmt->es_list);
        pmt->program_number = program_number;
        pmt->version = version;
        pmt->current_next = current_next;
        pmt->pcr_pid = pcr_pid;
    }

    return pmt;
}

struct tssp_pmt_es *tssp_pmt_add_es(struct tssp_pmt *pmt,
                                    uint8_t type,
                                    uint16_t pid)
{
    struct tssp_pmt_es *es;

    TSSP_ASSERT(pmt);

    es = malloc(sizeof(*es));
    if (es) {
        STAILQ_INIT(&es->descriptors);
        es->type = type;
        es->pid = pid;
        STAILQ_INSERT_TAIL(&pmt->es_list, es, entry);
    }

    return es;
}

void tssp_pmt_destroy(struct tssp_pmt *pmt)
{
    struct tssp_pmt_es *tmp;

    if (pmt) {
        tssp_descriptors_destroy(&pmt->descriptors);
        while ((tmp = STAILQ_FIRST(&pmt->es_list))) {
            STAILQ_REMOVE_HEAD(&pmt->es_list, entry);
            tssp_pmt_es_destroy(tmp);
        }
        free(pmt);
    }
}

void tssp_pmt_es_destroy(struct tssp_pmt_es *es)
{
    if (es) {
        tssp_descriptors_destroy(&es->descriptors);
        free(es);
    }
}


static
void tssp_pmt_monitor_section_callback(struct tssp_table_monitor *tmon,
                                       struct tssp_section *section,
                                       void *userdata)
{
    struct tssp_pmt_monitor *mon = userdata;
    struct tssp_pmt *pmt;

    pmt = tssp_pmt_parse(section);
    if (pmt)
        mon->callback(mon, pmt, section, mon->userdata);
    else
        tssp_section_destroy(section);
}

struct tssp_pmt_monitor *
tssp_pmt_monitor_attach(struct tssp *parser,
                        uint16_t program_number,
                        uint16_t pid,
                        uint8_t current_next,
                        tssp_pmt_monitor_callback callback,
                        void *userdata)
{
    struct tssp_pmt_monitor *monitor;
    struct tssp_section_filter filter;

    if (parser == NULL || callback == NULL)
        return NULL;

    monitor = malloc(sizeof(*monitor));
    if (monitor == NULL)
        return NULL;

    tssp_section_filter_init(&filter, pid, TSSP_TABLE_ID_PMT,
                             program_number, 0xFFFF, 1, 0);

    monitor->program_number = program_number;
    monitor->callback = callback;
    monitor->userdata = userdata;
    monitor->tmon = tssp_table_monitor_attach(parser,
                                              &filter,
                                              tssp_pmt_monitor_section_callback,
                                              monitor);
    if (monitor->tmon == NULL) {
        free(monitor);
        return NULL;
    }

    return monitor;
}

void tssp_pmt_monitor_detach(struct tssp_pmt_monitor *monitor)
{
    if (monitor == NULL)
        return;

    TSSP_ASSERT(monitor->tmon);

    tssp_pmt_monitor_reset(monitor);
    tssp_table_monitor_detach(monitor->tmon);
    free(monitor);
}

void tssp_pmt_monitor_reset(struct tssp_pmt_monitor *mon)
{
    TSSP_ASSERT(mon);
    tssp_table_monitor_reset(mon->tmon);
}

